include $(TOPDIR)/rules.mk

PKG_NAME:=chinadns
PKG_VERSION:=1.3.2
PKG_RELEASE=1

PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/shadowsocks/ChinaDNS.git
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)
PKG_SOURCE_VERSION:=9e4261c78e911851f76a9b9113d48fcdc44ce32e
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_LICENSE:=GPLv3
PKG_LICENSE_FILES:=COPYING
PKG_MAINTAINER:=clowwindy <clowwindy42@gmail.com>
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)

# PKG_SOURCE_URL:=https://github.com/clowwindy/ChinaDNS/releases/download/$(PKG_VERSION)
# PKG_SOURCE:=chinadns-$(PKG_VERSION).tar.gz
# PKG_MAINTAINER:=clowwindy <clowwindy42@gmail.com>
# PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)

PKG_INSTALL:=1
PKG_FIXUP:=autoreconf
PKG_USE_MIPS16:=0
PKG_BUILD_PARALLEL:=1

include $(INCLUDE_DIR)/package.mk

define Package/chinadns/Default
SECTION:=net
CATEGORY:=Network
TITLE:=ChinaDNS
URL:=https://github.com/clowwindy/ChinaDNS-C
endef

define Package/chinadns
	$(call Package/chinadns/Default)
endef

define Package/chinadns/description
A DNS forwarder that ignores incorrect(you knew it) responses.
endef

define Package/chinadns/conffiles
	/etc/ipset/china
	/etc/ipset/fake
endef

define Package/chinadns/postinst
#!/bin/sh
( . /etc/uci-defaults/chinadns ) && rm -f /etc/uci-defaults/chinadns
exit 0
endef

define Package/chinadns/prerm
#!/bin/sh
uci -q batch <<-EOF >/dev/null
	delete firewall.chinadns
	commit firewall
EOF
/etc/init.d/chinadns disable >/dev/null 2>&1
exit 0
endef

define Package/chinadns/install
	$(CP) ./root/* $(1)
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/src/chinadns $(1)/usr/bin
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/chinadns.init $(1)/etc/init.d/chinadns
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_DATA) ./files/chinadns.config $(1)/etc/config/chinadns
	$(INSTALL_DIR) $(1)/etc
	$(INSTALL_BIN) ./files/chinadns.firewall $(1)/etc/firewall.chinadns
	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_BIN) ./files/chinadns.uci $(1)/etc/uci-defaults/chinadns
endef

$(eval $(call BuildPackage,chinadns))
